<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * conlabzCMS
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Article
 *
 * Allow the user to manage website articles
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Article extends User_Controller {
	function Article( ) {
		parent::User_Controller( );

		$this->load->helper( 'form' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'article' ), 'article/user/article' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Article' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the article model
		$this->load->model( 'article_model' );
		$this->load->model( 'website_model' );
		
		// Load the article language module
		$this->lang->module_load( 'article', 'article' );
		
		log_message( 'debug', 'conlabz CMS : Article class loaded' );
	}

	/**
	 * View Articles
	 *
	 * @access public
	 */
	function index( ) {
		// Get Article Infomation
		$data['articles'] = $this->article_model->getArticles( " article.category_language_id = '1'" );

		// Display Page
		$data['header'] = $this->lang->line( 'article_overview' );
		$data['page']   = $this->config->item('backendpro_template_user') . "/view_article";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
	}

	/**
	 * Get Article Details
	 *
	 * Load article detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_article_details( ) {
		$data['id']            = $this->input->post( 'id'            );
		$data['teaser']        = $this->input->post( 'teaser'        );
		$data['active']        = $this->input->post( 'active'        );
		$data['url_title']     = $this->input->post( 'url_title'     );
		$data['body_text']     = $this->input->post( 'body_text'     );
		$data['menu_title']    = $this->input->post( 'menu_title'    );	
		$data['category_id']   = $this->input->post( 'category_id'   );
		$data['article_title'] = $this->input->post( 'article_title' );

		return $data;
	}

	/**
	 * Display Article Form
	 *
	 * @access public
	 * @param integer $id Article ID
	 */
	function form( $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']            = "ID";
		$fields['teaser']        = $this->lang->line( 'article_teaser'        );
		$fields['active']        = $this->lang->line( 'article_active'        );
		$fields['category']      = $this->lang->line( 'article_category'      );
		$fields['body_text']     = $this->lang->line( 'article_body_text'     );
		$fields['article_title'] = $this->lang->line( 'article_article_title' );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create article rules (make sure no-one has the same short name)
			$rules['teaser']        = "trim";
			$rules['body_text']     = "trim|required";
			$rules['article_title'] = "trim|required";
		} else {
			// Use edit article rules (make sure no-one other than the current user has the same short name)
			$rules['teaser']        = "trim";
			$rules['body_text']     = "trim|required";
			$rules['article_title'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$article = $this->article_model->getArticles( "article.id = " . $id . " AND category_language_id = '1'" );
			$article = $article->row_array( );
			$this->validation->set_default_value( $article );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category_id', $this->preference->item( 'article_category' ) );
			$this->validation->set_default_value( 'active', '1' );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			$data['language_id'] = '';
			// Construct Category dropdown
			$where = " AND website_id = " . $this->session->userdata( 'website_id' ) . " AND language_id = '1'";
			$this->load->model( 'category_model' );
			$data['category'] = $this->category_model->buildCategoryDropdown( 'category', 'id', $where );
			// Display form
			$this->validation->output_errors( );
			//@todo: remove!!!
			$_SESSION['path'] = "trassenheide";
			$data['fckeditor'] = array(
              	'name'        => 'body_text',
              	'id'          => 'body_text',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config' => array( 'CustomConfigurationsPath' => DEVPATH.'template/'.$this->session->userdata( 'template' ).'/js/fckconfig.js' )
			);
    		
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_article' ) : $this->lang->line( 'edit_article' ) );
			$this->bep_site->set_crumb( $data['header'],'article/user/article/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_article";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$data['referer'] = 'form';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$article = $this->_get_article_details( );
				$article['timestamp'] = date( 'Y-m-d H:i:s' );
				//set category_language_id
				$article['category_language_id'] = 1;

				$this->db->trans_start( );
				$this->article_model->insert( $article );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $article['article_title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_user') ) );
				}
				redirect( 'article/user/article' );
			} else {
				// SAVE
				$article = $this->_get_article_details( );
				$article['timestamp'] = date( 'Y-m-d H:i:s' );
				//set category_language_id
				$article['category_language_id'] = 1;
				
				$this->db->trans_start( );
				$this->article_model->update( $article, $article['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'article_saved' ), $article['article_title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect('article/user/article');
			}
		}
	}
	

	/**
	 * Article translation form
	 *
	 * @access public
	 * @param integer $id Article ID
	 */
	function translate( $id, $language_id ) {
		//check between insert and update mode
		$article = $this->article_model->getArticles( 'article.id = ' . $id  . " AND category_language_id = '" . $language_id . "'");
		$article = $article->row_array( );
		
		if( empty( $article ) ) {
			//no translation yet
			$translation = NULL;
		} else {
			$translation = 1;
		}
		
		// VALIDATION FIELDS
		$fields['id']            = "ID";
		$fields['teaser']        = $this->lang->line( 'article_teaser'        );
		$fields['active']        = $this->lang->line( 'article_active'        );
		$fields['category']      = $this->lang->line( 'article_category'      );
		$fields['body_text']     = $this->lang->line( 'article_body_text'     );
		$fields['article_title'] = $this->lang->line( 'article_article_title' );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $translation ) ) {
			// Use create article rules (make sure no-one has the same short name)
			$rules['teaser']        = "trim";
			$rules['body_text']     = "trim|required";
			$rules['article_title'] = "trim|required";
		} else {
			// Use edit article rules (make sure no-one other than the current user has the same short name)
			$rules['teaser']        = "trim";
			$rules['body_text']     = "trim|required";
			$rules['article_title'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $translation ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$this->validation->set_default_value( $article );
		} elseif( is_null( $translation ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category_id', $this->preference->item( 'article_category' ) );
			$this->validation->set_default_value( 'id', $id );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}
			
		// RUN
		if( $this->validation->run( ) === FALSE) {
			$article = $this->article_model->getArticles( 'article.id = ' . $id  . " AND category_language_id = '1'");
			$data['original'] = $article->row_array( );
			$data['language_id'] = $language_id;
			// Construct Category dropdown
			$where = " AND website_id = " . $this->session->userdata( 'website_id' ) . " AND language_id = '" . $language_id . "'";
			$this->load->model( 'category_model' );
			$data['category'] = $this->category_model->buildCategoryDropdown( 'category', 'id', $where );
			// Display form
			$this->validation->output_errors( );
			//@todo: remove trassenheide!!!
			$_SESSION['path'] = "trassenheide";
			$data['fckeditor'] = array(
              	'name'        => 'body_text',
              	'id'          => 'body_text',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config' => array( 'CustomConfigurationsPath' => DEVPATH.'template/'.$this->session->userdata( 'template' ).'/js/fckconfig.js' )
			);
    		
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_article' ) : $this->lang->line( 'edit_article' ) );
			$this->bep_site->set_crumb( $data['header'],'article/user/article/translate/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_article";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$data['referer'] = 'translate';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $translation ) ) {
				// CREATE - new translation
				// Fetch form values
				$article = $this->_get_article_details( );
				$article['timestamp'] = date( 'Y-m-d H:i:s' );
				$article['category_language_id'] = $language_id;

				$this->db->trans_start( );
				$this->article_model->insert( $article );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $article['article_title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_user') ) );
				}
				redirect( 'article/user/article' );
			} else {
				// SAVE
				$article = $this->_get_article_details( );
				$article['timestamp'] = date( 'Y-m-d H:i:s' );
				$article['category_language_id'] = $language_id;
				
				$this->db->trans_start( );
				$this->article_model->update( $article, $article['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'article_saved' ), $article['article_title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect('article/user/article');
			}
		}
	}

	/**
	 * Delete
	 *
	 * Delete the selected article from the system
	 *
	 * @access public
	 */
	function delete( ) {	
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'article/user/article', 'location' );
		}

		foreach( $selected as $article ) {
			$this->article_model->delete( $article );
		}

		flashMsg( 'success', $this->lang->line( 'article_deleted' ) );
		redirect( 'article/user/article', 'location' );
	}
}
/* End of file article.php */
/* Location: ./modules/article/controllers/user/article.php */
